//
//  HomeTableView.m
//  one·一个  tyw
//
//  Created by tyw on 15/8/31.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "HomeTableView.h"
#import "DayNightLabel.h"
#import "Tools.h"
#import "DayNightImgView.h"
#import "ZoomImgView.h"
#import "UIImageView+WebCache.h"

@implementation HomeTableView {
    DayNightLabel* likeCountLabel;      //赞数量显示
    
    UIButton* likeBtn;                  //点赞按钮
}

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        self.backgroundColor = [UIColor clearColor];
        
        self.delegate = self;
        self.dataSource = self;
        
        
        //接收通知，每次cell display的时候，就刷新是否点赞这一UI
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cellDisplayNotificationAction:) name:kCollectionCellWillDisplayNotification object:nil];
    }
    
    return self;
}

#pragma mark - notificationAction
- (void)cellDisplayNotificationAction:(NSNotification*)notification  {
    NSDictionary* dic = notification.userInfo;
    
    BOOL praised = [dic[@"praised"] boolValue];
    
    if (praised) {
        likeBtn.selected = YES;
    } else {
        likeBtn.selected = NO;
    }
}

#pragma mark - setDataDic
- (void)setDataDic:(NSDictionary *)dataDic {
    _dataDic = dataDic;
    [self reloadData];
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    if (indexPath.row == 0) {
        NSString* vol = _dataDic[@"strHpTitle"];
        DayNightLabel* label = [[DayNightLabel alloc] initWithFrame:CGRectMake(5, 5, 100, cell.bounds.size.height-10)];
        label.text = vol;
        [label loadLabelColor];
        label.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:label];
    } else if (indexPath.row == 1) {
        ZoomImgView* imgView = [[ZoomImgView alloc] initWithFrame:CGRectMake(5, 5, kWidth-10, kHeight/2-10)];
//        NSString* imgUrl = _dataDic[@"strOriginalImgUrl"];
//        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
//        imgView.image = [UIImage imageWithData:data];
        NSString* imgUrl = _dataDic[@"strOriginalImgUrl"];
        if (imgUrl.length > 0) {
            [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        }
        imgView.contentMode = UIViewContentModeScaleAspectFit;
//        imgView.backgroundColor = [UIColor redColor];
        [cell.contentView addSubview:imgView];
    } else if (indexPath.row == 2) {
        DayNightLabel* paintNameLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(kWidth-205, -10, 200, 20)];
        paintNameLabel.textAlignment = NSTextAlignmentRight;
        paintNameLabel.font = [UIFont systemFontOfSize:12];
        [paintNameLabel loadLabelColor];
        [cell.contentView addSubview:paintNameLabel];
        DayNightLabel* makerNameLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(kWidth-205, 10, 200, 20)];
        makerNameLabel.textAlignment = NSTextAlignmentRight;
        makerNameLabel.font = [UIFont systemFontOfSize:12];
        [makerNameLabel loadLabelColor];
        [cell.contentView addSubview:makerNameLabel];
        
        NSString* strAuthor = _dataDic[@"strAuthor"];
        NSArray* array = [strAuthor componentsSeparatedByString:@"&"];
        if (array.count >= 2) {
            NSString* paintName = array[0];
            NSString* makerName = array[1];
            paintNameLabel.text = paintName;
            makerNameLabel.text = makerName;
        } else {
            NSLog(@"画作 数据有误");
        }
    } else if (indexPath.row == 3) {
        //年月日
        NSString* dateStr = _dataDic[@"strMarketTime"];
        NSArray* array = [dateStr componentsSeparatedByString:@"-"];
        if (array.count >= 3) {
            NSString* yearStr = array[0];
            NSString* monthStr = [Tools exchangeMonth:array[1]];
            NSString* dayStr = array[2];
            
            UILabel* dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 80, 40)];
            dayLabel.text = dayStr;
            dayLabel.textAlignment = NSTextAlignmentCenter;
            dayLabel.textColor = [UIColor colorWithRed:71.0/255 green:181.0/255 blue:250.0/255 alpha:1];
            dayLabel.font = [UIFont systemFontOfSize:30];
            [cell.contentView addSubview:dayLabel];
            
            DayNightLabel* monthYearLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 40, 80, 20)];
            monthYearLabel.text = [NSString stringWithFormat:@"%@ , %@", monthStr, yearStr];
            monthYearLabel.font = [UIFont systemFontOfSize:10];
            monthYearLabel.textAlignment = NSTextAlignmentCenter;
            [monthYearLabel loadLabelColor];
            [cell.contentView addSubview:monthYearLabel];
        } else {
            NSLog(@"名言数据有误，可能是因为网络数据慢了半拍，刷新了表视图就好了");
        }
        
        //名言
        NSString* contentStr = _dataDic[@"strContent"];
        CGFloat height = [Tools contentSizeWithMaxWidth:kWidth-120 text:contentStr fontOfSize:15].height;
        DayNightImgView* imgView = [[DayNightImgView alloc] initWithFrame:CGRectMake(100, 5, kWidth-120, height+30)];
        [imgView setTopCapWidth:20];        //设置拉伸点,要先设置这个再设置imgName
        [imgView setLeftCapWidth:10];
        [imgView setImgName:@"contBack"];
        [cell.contentView addSubview:imgView];
        
        DayNightLabel* contentLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(12, 5, kWidth-130, height+20)];
        contentLabel.text = contentStr;
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.font = [UIFont systemFontOfSize:15];
        contentLabel.numberOfLines = 0;
        [contentLabel loadLabelColor];
        [imgView addSubview:contentLabel];
    } else {                //点赞
        UIImageView* likeBgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth-80, 30, 80, 30)];
        UIImage* image = [UIImage imageNamed:@"home_likeBg"];
        image = [image stretchableImageWithLeftCapWidth:40 topCapHeight:0];     //拉伸
        likeBgImgView.contentMode = UIViewContentModeScaleAspectFit;
        likeBgImgView.image = image;
        //likeBgImgView.backgroundColor = [UIColor redColor];
        likeBgImgView.userInteractionEnabled = YES;
        [cell.contentView addSubview:likeBgImgView];
        
        if (likeBtn == nil) {
            likeBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 8, 15, 15)];
        }
        [likeBtn setImage:[UIImage imageNamed:@"home_like"] forState:UIControlStateNormal];
        [likeBtn setImage:[UIImage imageNamed:@"home_like_hl"] forState:UIControlStateSelected];
        [likeBtn addTarget:self action:@selector(likeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [likeBgImgView addSubview:likeBtn];
        
        likeCountLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(30, 8, likeBgImgView.bounds.size.width-20, 15)];
        NSString* likeCount = _dataDic[@"strPn"];
        likeCountLabel.text = likeCount;
        likeCountLabel.textAlignment = NSTextAlignmentCenter;
        likeCountLabel.font = [UIFont systemFontOfSize:8];
        [likeCountLabel loadLabelColor];
        [likeBgImgView addSubview:likeCountLabel];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 40;
    } else if (indexPath.row == 1) {
        return kHeight/2;
    } else if (indexPath.row == 2) {
        return 50;
    } else if (indexPath.row == 3) {        //需要自适应
        NSString* contentStr = _dataDic[@"strContent"];
        CGFloat height = [Tools contentSizeWithMaxWidth:kWidth-120 text:contentStr fontOfSize:15].height;
        return height+40;
    } else {
        return 60;
    }
}

#pragma mark - btnAtion
- (void)likeBtnAction:(UIButton*)button {
    button.selected = !button.selected;
    
    NSInteger likeCount = [likeCountLabel.text integerValue];
    likeCount = button.selected?likeCount+1:likeCount-1;
    likeCountLabel.text = [NSString stringWithFormat:@"%li", (long)likeCount];
}

#pragma mark - dealloc
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kCollectionCellWillDisplayNotification object:nil];
}

@end
